<?php
namespace app\common\service;
use app\admin\traits\Result;
use think\facade\Request;
use think\facade\Env;
use pChart\pColor;
use pChart\pDraw;
use pChart\pCharts;
use pChart\pPie;

/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2022/7/13
 * Time: 17:29
 */
class PchartService
{

    public static function makepie($data){

        $kechuang = json_decode($data['kechuang'],true);

        /* Create the pChart object */
        //$myPicture = new pDraw(300,260);
        $myPicture = new pDraw(256,220);

        /* Populate the pData object */
        $myPicture->myData->addPoints([$data['score2'],$data['score']],"ScoreA");
        $myPicture->myData->setSerieDescription("ScoreA","Application A");

        /* Define the abscissa serie */
        $myPicture->myData->addPoints(["A0","B1"],"Labels");
        $myPicture->myData->setAbscissa("Labels");

        $myPicture->myData->setPalette('Labels',new pColor(59,133,167));//设置线条蓝颜色
        $myPicture->myData->setPalette('ScoreA',new pColor(169,169,157));//设置线条灰颜色

        /* Draw a solid background */
        $myPicture->drawFilledRectangle(0,0,256,220,["Color"=>new pColor(89,89,89), "Dash"=>TRUE, "DashColor"=>new pColor(89,89,89)]);

        /* Overlay with a gradient */
        /*$myPicture->drawGradientArea(0,0,300,260, DIRECTION_VERTICAL,["StartColor"=>new pColor(219,231,139,50),"EndColor"=>new pColor(1,138,68,50)]);
        $myPicture->drawGradientArea(0,0,300,20, DIRECTION_VERTICAL, ["StartColor"=>ColorBlack(), "EndColor"=>new pColor(50,50,50,100)]);*/

        /* Add a border to the picture */
        //边框
        //$myPicture->drawRectangle(0,0,299,259,["Color"=>ColorBlack()]);

        /* Write the picture title */
        $myPicture->setFontProperties(["FontName"=>Env::get('ROOT_PATH')."/vendor/bozhinov/pchart/pChart/fonts/1.ttf","FontSize"=>12]);
        $myPicture->drawText(88,210,$kechuang['result'],["Color"=>ColorWhite()]);
        $myPicture->drawText(108,115,'测评总分',["Color"=>new pColor(200,300,200,20),"FontSize"=>8]);
        $myPicture->drawText(111,135,$data['score'],["Color"=>ColorWhite(),"FontSize"=>12]);

        /* Set the default font properties */
        $myPicture->setFontProperties(["FontName"=>Env::get('ROOT_PATH')."/vendor/bozhinov/pchart/pChart/fonts/Forgotte.ttf","FontSize"=>10,"Color"=>new pColor(80,80,80)]);


        /* Enable shadow computing */
        //阴影
        //$myPicture->setShadow(TRUE,["X"=>2,"Y"=>2,"Color"=>ColorBlack($Alpha=50)]);

        /* Create the pPie object */
        $PieChart = new pPie($myPicture);

        /* Draw an AA pie chart */ //调整图形大小"OuterRadius"=>60,'InnerRadius'=>40,
        $PieChart->draw2DRing(130,120,["OuterRadius"=>60,'InnerRadius'=>40,"DrawLabels"=>FALSE,"LabelStacked"=>TRUE,"Border"=>FALSE]);


        /* Write the legend box */
        $myPicture->setShadow(FALSE);
        //图例
        //$PieChart->drawPieLegend(15,40,["Color"=>new pColor(200,200,200,20)]);

        /* Render the picture (choose the best way) */
        $imageFile = "/images/chart/".$data['user_id'].'_'.$data['a_id']."_pie.png";
        $myPicture->Render(Env::get('ROOT_PATH')."public".$imageFile);
        //echo '<img src="'.$imageFile.'">';
        return $imageFile;

    }


    public static function  makeLine($data){

        $chart1_json = json_decode($data['chart1_json'],true);
        $chart2_json = json_decode($data['chart2_json'],true);
        $chart1 = $chart2 = $chartx = [];
        foreach ($chart1_json as $k=>$v){
            $chart1[] = $v['y'];
            $chart2[] = $chart2_json[$k]['y'];
            $chartx[] = $v['label'];
        }
        //dump($chart1_json);dump($chart1); dump($chart2); dump($chartx);die;
        /* Create the pChart object */
        $myPicture = new pDraw(765,315);
        /* Create and populate the pData object *///实际值 参考值
        $myPicture->myData->addPoints($chart1,"实际值");//数据1
        $myPicture->myData->addPoints($chart2,"参考值");//数据2
        $myPicture->myData->setSerieTicks("参考值",4);
        //$myPicture->myData->setAxisName(0,"Temperatures");//纵坐标标题
        $myPicture->myData->addPoints($chartx,"Labels"); //横坐标标题
        $myPicture->myData->setSerieDescription("Labels","Months");
        $myPicture->myData->setAbscissa("Labels");

        $myPicture->myData->setPalette('实际值',new pColor(79,129,188));//设置线条颜色
        $myPicture->myData->setPalette('参考值',new pColor(192,80,78));//设置线条颜色

        /* Turn off Anti-aliasing */
        $myPicture->Antialias = FALSE;

        /* Draw the background */
        $myPicture->drawFilledRectangle(0,0,759,300,["Color"=>new pColor(255,255,255), "Dash"=>TRUE, "DashColor"=>new pColor(255,255,255)]);

        /* Overlay with a gradient */
        $myPicture->drawGradientArea(0,0,759,300,DIRECTION_VERTICAL, ["StartColor"=>new pColor(255,255,255,0.5),"EndColor"=>new pColor(255,255,255,0.5)]);

        /* Add a border to the picture */
        $myPicture->drawRectangle(0,0,759,300,["Color"=>new pColor(89,89,89)]);
        /* Write the chart title */
        $myPicture->setFontProperties(["FontName"=>Env::get('ROOT_PATH')."/vendor/bozhinov/pchart/pChart/fonts/1.ttf","FontSize"=>11]);
        $myPicture->drawText(150,35,"",["FontSize"=>20,"Align"=>TEXT_ALIGN_BOTTOMMIDDLE]);//图表名称

        /* Set the default font */
        $myPicture->setFontProperties(["FontName"=>Env::get('ROOT_PATH')."/vendor/bozhinov/pchart/pChart/fonts/1.ttf","FontSize"=>8]);

        /* Define the chart area */
        $myPicture->setGraphArea(20,10,759,300);

        /* Draw the scale */
        $myPicture->drawScale(["XMargin"=>0,"YMargin"=>0,"Floating"=>TRUE,"GridColor"=>new pColor(89,89,89,0.5),"DrawSubTicks"=>TRUE,"CycleBackground"=>TRUE]);

        /* Write the chart legend */
        $myPicture->drawLegend(540,20,["Style"=>LEGEND_NOBORDER,"Mode"=>LEGEND_HORIZONTAL]); //图例

        /* Turn on Anti-aliasing */
        $myPicture->Antialias = TRUE;

        /* Create the pCharts object */
        $pCharts = new pCharts($myPicture);

        /* Draw the area chart */
        $pCharts->myPicture->myData->setSerieDrawable("实际值",TRUE);
        $pCharts->myPicture->myData->setSerieDrawable("参考值",FALSE);
        $pCharts->drawAreaChart();

        /* Draw a line and a plot chart on top */
        $pCharts->myPicture->myData->setSerieDrawable("参考值",TRUE);
        $pCharts->myPicture->setShadow(TRUE,["X"=>1,"Y"=>1,"Color"=>new pColor(89,89,89,0.5)]);
        $pCharts->drawLineChart();
        $pCharts->drawPlotChart(["PlotBorder"=>TRUE,"PlotSize"=>3,"BorderSize"=>1,"Surrounding"=>-60, "BorderColor"=>new pColor(89,89,89,0.5)]);

        /* Render the picture (choose the best way) */

        //直接渲染显示图片
        //$myPicture->autoOutput();die;

        //生成图片
        $imageFile = "/images/chart/".$data['user_id'].'_'.$data['a_id']."_line.png";
        $myPicture->Render(Env::get('ROOT_PATH')."public".$imageFile);
        //echo '<img src="'.$imageFile.'">';
        return $imageFile;

    }
}